package com.caiyigo.baihua.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.ClientProtocolException;
import org.sageras.util.ExcelUtil.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.caiyigo.baihua.Const.Dictionary;
import com.caiyigo.baihua.dto.DataGrid;
import com.caiyigo.baihua.dto.KaoshicjDto;
import com.caiyigo.baihua.dto.Pager;
import com.caiyigo.baihua.dto.Result;
import com.caiyigo.baihua.model.Kaoshicj;
import com.caiyigo.baihua.model.Kaoshikemu;
import com.caiyigo.baihua.model.Xuesheng;
import com.caiyigo.baihua.service.KaoshicjService;
import com.caiyigo.baihua.service.KaoshikemuService;
import com.caiyigo.baihua.service.XueshengService;
import com.github.miemiedev.mybatis.paginator.domain.Order;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

@Controller
@RequestMapping(value = "/business/kaoshicj")
public class KaoshicjController {
	@Autowired
	KaoshicjService kaoshicjService;
	@Autowired
	XueshengService xueshengService;
	@Autowired
	KaoshikemuService kaoshikemuService;

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public @ResponseBody DataGrid<KaoshicjDto> list(KaoshicjDto dto, Pager page) {
		List<Order> orders = new ArrayList<Order>();
		orders.add(Order.create(KaoshicjDto.FileConstant.zkz, "desc"));
		orders.add(Order.create(KaoshicjDto.FileConstant.pici, "desc"));
		// orders.add(Order.create(page.getSort(), page.getOrder()));
		PageList<KaoshicjDto> pageList = (PageList<KaoshicjDto>) kaoshicjService
				.getList(dto, new PageBounds(page.getPage(), page.getRows(),
						orders, true));
		for (KaoshicjDto kaoshicjDto : pageList) {
			kaoshicjDto.setIsSendReason(Dictionary.FSZTYY.getName(kaoshicjDto
					.getIsSendReason()));
			kaoshicjDto.setIsSend(Dictionary.FSZT.getName(kaoshicjDto
					.getIsSend()));
		}
		return new DataGrid<KaoshicjDto>(pageList.getPaginator()
				.getTotalCount(), pageList.getPaginator().getPage(), pageList);

	}

	@RequestMapping(value = "/export", method = RequestMethod.GET)
	public void export(KaoshicjDto dto, Pager page, HttpServletResponse response) {
		response.setCharacterEncoding("UTF-8");
		String fileName = "baihua-kaoshicj-" + System.currentTimeMillis()
				+ ".xls";
		List<Order> orders = new ArrayList<Order>();
		orders.add(Order.create(KaoshicjDto.FileConstant.zkz, "desc"));
		List<KaoshicjDto> KaoshicjDtopageList = kaoshicjService.getList(dto,
				new PageBounds(orders));
		for (KaoshicjDto kaoshicjDto : KaoshicjDtopageList) {
			kaoshicjDto.setIsSend(Dictionary.FSZTYY.getName(kaoshicjDto
					.getIsSend()));
			kaoshicjDto.setIsSendReason(Dictionary.FSZTYY.getName(kaoshicjDto
					.getIsSendReason()));
		}
		response.setHeader("Content-Disposition", "inline; filename="
				+ fileName);
		// 确保发送的当前文本格式
		response.setContentType("application/vnd.ms-excel");
		try {
			ExcelUtil.exportExcel("百花教育考试成绩表", KaoshicjDtopageList,
					response.getOutputStream(), KaoshicjDto.class);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	@RequestMapping(value = "/select", method = RequestMethod.POST)
	public @ResponseBody DataGrid<KaoshicjDto> select(KaoshicjDto dto,
			DataGrid<KaoshicjDto> dataGrid) {
		List<Order> orders = new ArrayList<Order>();
		orders.add(Order.create(dataGrid.getSort(), dataGrid.getOrder()));
		PageList<KaoshicjDto> pageList = (PageList<KaoshicjDto>) kaoshicjService
				.getList(dto, new PageBounds(1, 11, orders, true));
		dataGrid.setPage(pageList.getPaginator().getPage());
		dataGrid.setRows(pageList);
		return dataGrid;
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody Result delete(
			@RequestParam(value = "id", required = true) Integer id) {
		return new Result(kaoshicjService.deleteByPrimaryKey(id) > 0);
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public @ResponseBody Result add(@ModelAttribute("user") KaoshicjDto dto) {

		return new Result(kaoshicjService.insertSelective(dto) > 0);

	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public @ResponseBody Result edit(@ModelAttribute("user") KaoshicjDto dto) {

		return new Result(kaoshicjService.updateByPrimaryKeySelective(dto) > 0);

	}

	@RequestMapping(value = "/batchDelete", method = RequestMethod.GET)
	public @ResponseBody Result batchDelete(
			@RequestParam(value = "ids", required = false) String id) {
		String[] idArray = id.split(",");

		return new Result(kaoshicjService.batchDelete(idArray) > 0);

	}

	@RequestMapping(value = "/getcj", method = RequestMethod.POST)
	public @ResponseBody Result getcj(String pici)
			throws ClientProtocolException, IOException {
		Map<String, Xuesheng> xueMap = xueshengService.getAll();

		Result result = CjZwUtil.get(xueshengService.getAll(), pici, true,
				false);
		List<Future<List<Object>>> resultFutureList = (List<Future<List<Object>>>) result
				.getData();
		List<KaoshicjDto> list = new ArrayList<KaoshicjDto>();
		for (Future<List<Object>> future : resultFutureList) {

			List<Object> objects = null;

			try {
				objects = future.get();
				if (null != objects) {
					for (Object object : objects) {
						KaoshicjDto cjDto = (KaoshicjDto) object;
						if (StringUtils.isNotEmpty(cjDto.getZkz())) {
							cjDto.setXsid(xueMap.get(cjDto.getZkz()).getId());

							list.add(cjDto);
						}
						System.out.println(cjDto);
					}
				}

			} catch (InterruptedException e) {

				e.printStackTrace();
				continue;
			} catch (ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				continue;
			}

		}
		if (list.size() > 0) {
			List<Kaoshikemu> kaoshicjs = new ArrayList<Kaoshikemu>();

			for (KaoshicjDto kaoshicjDto : list) {

				kaoshicjs.add(new Kaoshikemu(kaoshicjDto.getKmid(), kaoshicjDto
						.getKmmc()));
			}
			kaoshicjService.batchUpdate(list);
			kaoshikemuService.batchUpdate(kaoshicjs);
		}

		return new Result(true);

	}

	@RequestMapping(value = "/getcjByXsid", method = RequestMethod.POST)
	public @ResponseBody Result getcj(String pici, Integer xsid) {
		Xuesheng xuesheng = xueshengService.selectByPrimaryKey(xsid);
		Map<String, Xuesheng> xueMap = new HashMap<String, Xuesheng>();
		xueMap.put(xuesheng.getZkz(), xuesheng);
		try {
			Result result = CjZwUtil.get(xueMap, pici, false, false);
			List<Future<List<Object>>> resultFutureList = (List<Future<List<Object>>>) result
					.getData();
			List<KaoshicjDto> list = new ArrayList<KaoshicjDto>();
			for (Future<List<Object>> future : resultFutureList) {

				List<Object> objects = null;

				try {
					objects = future.get();
					if (null != objects) {
						for (Object object : objects) {
							KaoshicjDto cjDto = (KaoshicjDto) object;
							if (StringUtils.isNotEmpty(cjDto.getZkz())) {
								cjDto.setXsid(xueMap.get(cjDto.getZkz())
										.getId());

								list.add(cjDto);
							}
							System.out.println(cjDto);
						}
					}

				} catch (InterruptedException e) {

					e.printStackTrace();
					continue;
				} catch (ExecutionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					continue;
				}

			}
			if (list.size() > 0) {
				List<Kaoshikemu> kaoshicjs = new ArrayList<Kaoshikemu>();

				for (KaoshicjDto kaoshicjDto : list) {

					kaoshicjs.add(new Kaoshikemu(kaoshicjDto.getKmid(),
							kaoshicjDto.getKmmc()));
				}
				kaoshicjService.batchUpdate(list);
				kaoshikemuService.batchUpdate(kaoshicjs);
			}

			return new Result(true);

		} catch (ClientProtocolException e) {
			return new Result(false);
		} catch (IOException e) {
			return new Result(false);
		}
	}
}
